import { assign } from 'lodash'
import {
  fetchTableData,
  fetchTreeMap,
  fetchKXData,
  fetchAbnormalData,
  fetchXxlhData,
  fetchYqltjData,
  fetchXsyqmsData,
  fetchCdrkData,
  fetchLyqltjData,
  fetchDsycData,
  fetchSjqsData,
  fetchCcfxData,
  fetchSaveParams,
  fetchLoadData,
  fetchErrorTypes
} from '@/api/historyVerify'
import { getMeterAndUserInfo } from '@/api/meterDetail'

const defaultState = () => {
  return {
    defaultDate: undefined,
    tableData: null,
    tableLoading: false,
    tableHint: '',
    tablePageParams: {
      pageNum: 1,
      pageSize: 20
    },
    userQueryParams: {
      unitId: undefined,
      unitName: undefined
    },
    stopTableKeyUp: false,
    treeMapList: [],
    currentMeterId: undefined,
    currentGasCls: undefined,
    KXList: [],
    KXLoading: false,
    showZero: false,
    hasRemoveLines: false,
    hasRemoveSolid: false,
    qcycstForm: {
      min: 0,
      max: 0
    },
    hasNearAnomaly: false,
    nearAnomalyTimeList: [],
    selectedCandlSerie: undefined,
    abnormalList: [],
    xxlhList: [],
    xxlhParams: [],
    xxlhListIndexs: [],
    xxlhState: false,
    lxlyqlState: false,
    lxlyqlDay: 0,
    slgjState: false,
    slgjList: [],
    yqtjList: [],
    wbsjSelectState: false,
    cdrkList: [],
    showCdrkModal: false,
    currentType: undefined,
    byxs: null,
    selectedTableRow: undefined,
    loadState: false,
    spareGas: false,
    spareGasLine: false
  }
}

export default {
  namespaced: true,
  name: 'historyVerify',
  state: defaultState(),
  getters: {
    getTablePageParams: state => {
      return {
        ...state.tablePageParams
      }
    }
  },
  mutations: {
    REST_STATE(state) {
      state = assign(state, defaultState())
    },
    /**
     * desc: 设置表格数据
     * param {type}
     * return {type} void
     */
    SET_TABLE_DATA(state, tableData) {
      state.tableData = tableData
    },
    /**
     * desc: 设置表格加载状态
     * param {type}
     * return {type} void
     */
    SET_TABLE_LOADING(state, flag) {
      state.tableLoading = flag
    },
    /**
     * desc: 设置矩形树图数据
     * param {type}
     * return {type} void
     */
    SET_TREEMAP_DATA(state, data) {
      state.treeMapList = data
    },
    SET_CURRENT_METERID(state, meterId) {
      state.currentMeterId = meterId
    },
    /**
     * desc: 设置表格键盘操作标识符
     * param {type}
     * return {type} void
     */
    SET_STOP_TABLE_KEYUP(state, flag) {
      state.stopTableKeyUp = flag
    },
    /**
     * desc: 设置表格请求页数
     * param {type}
     * return {type} void
     */
    SET_TABLE_PAGE_NUM(state, num) {
      state.tablePageParams.pageNum = num
    },
    SET_KX_LIST(state, list) {
      state.KXList = list
    },
    SET_KX_LOADING(state, flag) {
      state.KXLoading = flag
    },
    SER_SHOW_ZERO(state, flag) {
      state.showZero = flag
    },
    SET_CURRENT_GAS_CLs(state, type) {
      state.currentGasCls = type
    },
    SET_HAS_REMOVE_LINES(state, flag) {
      state.hasRemoveLines = flag
    },
    SET_HAS_REMOVE_SOLID(state, flag) {
      state.hasRemoveSolid = flag
    },
    SET_QCYCST_FORM(state, data = {}) {
      state.qcycstForm = {
        ...state.qcycstForm,
        ...data
      }
    },
    SET_NEAR_ANOMALY_TIME_LIST(state, list) {
      state.nearAnomalyTimeList = list
    },
    SET_HAS_NEAR_ANOMALY(state, flag) {
      state.hasNearAnomaly = flag
    },
    SET_SELECTED_CANDLSERIE(state, time) {
      state.selectedCandlSerie = time
    },
    SET_ABNORMAL_DATA(state, data) {
      state.abnormalList = data
    },
    SET_XXLH_LIST(state, list) {
      state.xxlhList = list
    },
    SET_XXLH_PARAMS(state, list) {
      state.xxlhParams = list
    },
    SET_LXLYQL_STATE(state, flag) {
      state.lxlyqlState = flag
    },
    SET_LXLYQL_DAY(state, day) {
      state.lxlyqlDay = day
    },
    SET_XXLH_LIST_INDEXS(state, list) {
      state.xxlhListIndexs = list
    },
    SET_SLGJ_STATE(state, flag) {
      state.slgjState = flag
    },
    SET_SLGJ_LIST(state, list) {
      state.slgjList = list
    },
    SET_XXLH_STATE(state, flag) {
      state.xxlhState = flag
    },
    SET_YQTJ_LIST(state, list) {
      state.yqtjList = list
    },
    SET_USER_QUERY_PARAMS(state, data) {
      state.userQueryParams = {
        ...state.userQueryParams,
        ...data
      }
    },
    SET_WBSJ_SELECT_STATE(state, flag) {
      state.wbsjSelectState = flag
    },
    SET_CDRK_LIST(state, list) {
      state.cdrkList = list
    },
    SET_CDRK_MODAL(state, flag) {
      state.showCdrkModal = flag
    },
    SET_CURRENT_TYPE(state, type) {
      state.currentType = type
    },
    SET_BYXS(state, num) {
      state.byxs = num
    },
    SET_SELECTED_TABLE_ROW(state, index) {
      state.selectedTableRow = index
    },
    SET_LOAD_STATE(state, flag) {
      state.loadState = flag
    },
    SET_SPARE_GAS(state, flag) {
      state.spareGas = flag
    },
    SET_SPARE_GAS_LINE(state, flag) {
      state.spareGasLine = flag
    }
  },
  actions: {
    clearStore(ctx) {
      ctx.commit('REST_STATE')
    },
    /**
     * desc: 获取表格数据
     * param {type}
     * return {type} void
     */
    async loadTableData(ctx, params) {
      ctx.commit('SET_TABLE_LOADING', true)
      ctx.commit('SET_KX_LIST', [])
      ctx.commit('SET_CURRENT_METERID', undefined)
      const _tableData = await fetchTableData({
        ...ctx.state.tablePageParams,
        ...params
      })
      ctx.commit('SET_TABLE_LOADING', false)
      const { code, date = {} } = _tableData
      if (code === 200) {
        ctx.commit('SET_TABLE_DATA', date)
      }
    },
    /**
     * desc: 修改表格页码
     * param {type}
     * return {type} void
     */
    changeTablePageNum(ctx, num) {
      ctx.commit('SET_TABLE_PAGE_NUM', num)
    },
    /**
     * desc: 获取矩形树图
     * param {type}
     * return {type} void
     */
    async loadTreeMapData(ctx) {
      const { code, date } = await fetchTreeMap()
      if (code === 200) {
        ctx.commit('SET_TREEMAP_DATA', date)
      }
    },
    changeCurrentMeterId(ctx, meterId) {
      ctx.commit('SET_CURRENT_METERID', meterId)
      ctx.commit('SET_ABNORMAL_DATA', [])
      ctx.commit('SET_SELECTED_CANDLSERIE', undefined)
      ctx.commit('SET_HAS_REMOVE_LINES', false)
      ctx.commit('SET_HAS_REMOVE_SOLID', false)
      ctx.commit('SER_SHOW_ZERO', false)
      ctx.commit('SET_NEAR_ANOMALY_TIME_LIST', [])
      ctx.commit('SET_XXLH_LIST', [])
      ctx.commit('SET_HAS_NEAR_ANOMALY', false)
      ctx.commit('SET_LXLYQL_STATE', false)
      ctx.commit('SET_LXLYQL_DAY', 0)
      ctx.commit('SET_XXLH_LIST_INDEXS', [])
      ctx.commit('SET_SLGJ_STATE', false)
      ctx.commit('SET_XXLH_PARAMS', [])
      ctx.commit('SET_SLGJ_LIST', [])
      ctx.commit('SET_XXLH_STATE', false)
      ctx.commit('SET_WBSJ_SELECT_STATE', false)
      ctx.commit('SET_CDRK_MODAL', false)
      ctx.commit('SET_YQTJ_LIST', [])
      ctx.commit('SET_CDRK_LIST', [])
      ctx.commit('SET_QCYCST_FORM', {
        min: 0,
        max: 0
      })
      ctx.commit('SET_CURRENT_TYPE', undefined)
      ctx.commit('SET_BYXS', null)
      ctx.commit('SET_SPARE_GAS', false)
      ctx.commit('SET_SPARE_GAS_LINE', false)
    },
    changeLoadState(ctx, flag) {
      ctx.commit('SET_LOAD_STATE', flag)
      ctx.commit('SET_SELECTED_CANDLSERIE', undefined)
      ctx.commit('SET_HAS_REMOVE_LINES', false)
      ctx.commit('SET_HAS_REMOVE_SOLID', false)
      ctx.commit('SER_SHOW_ZERO', false)
      ctx.commit('SET_NEAR_ANOMALY_TIME_LIST', [])
      ctx.commit('SET_XXLH_STATE', false)
      ctx.commit('SET_WBSJ_SELECT_STATE', false)
      ctx.commit('SET_CDRK_MODAL', false)
      ctx.commit('SET_SLGJ_LIST', [])
    },
    /**
     * desc: 是否允许表格键盘操作
     * param {type}
     * return {type} void
     */
    changeStopTableKeyUp(ctx, flag) {
      ctx.commit('SET_STOP_TABLE_KEYUP', flag)
    },
    async loadKXData(ctx, params) {
      ctx.commit('SET_KX_LOADING', true)
      const { code, date } = await fetchKXData({
        meterId: ctx.state.currentMeterId,
        ...params
      })
      ctx.commit('SET_KX_LOADING', false)
      if (code === 200) {
        ctx.commit('SET_KX_LIST', date)
      }
    },
    changeKXList(ctx, data) {
      ctx.commit('SET_KX_LIST', data)
    },
    async loadMeterDetail(ctx) {
      const _ret = await getMeterAndUserInfo(ctx.state.currentMeterId)
      return _ret
    },
    changeShowZero(ctx, flag) {
      ctx.commit('SER_SHOW_ZERO', flag)
    },
    changeCurrentGasCls(ctx, type) {
      ctx.commit('SET_CURRENT_GAS_CLs', type)
    },
    changeHasRemoveLines(ctx, flag) {
      ctx.commit('SET_HAS_REMOVE_LINES', flag)
    },
    changeHasRemoveSolid(ctx, flag) {
      ctx.commit('SET_HAS_REMOVE_SOLID', flag)
    },
    changeQCYCSTFrom(ctx, data) {
      ctx.commit('SET_QCYCST_FORM', data)
    },
    changeNearAnomalyTimeList(ctx, list) {
      ctx.commit('SET_NEAR_ANOMALY_TIME_LIST', list)
    },
    changeHasNearAnomaly(ctx, flag) {
      ctx.commit('SET_HAS_NEAR_ANOMALY', flag)
    },
    changeSelectedCandlSerie(ctx, time) {
      ctx.commit('SET_SELECTED_CANDLSERIE', time)
    },
    async loadAbnormalData(ctx, params) {
      const { code, date } = await fetchAbnormalData({
        meterId: ctx.state.currentMeterId,
        days: ctx.state.selectedCandlSerie,
        ...params
      })
      if (code === 200) {
        ctx.commit('SET_ABNORMAL_DATA', date)
      }
    },
    async loadXxlhData(ctx, data) {
      const { code, date } = await fetchXxlhData({
        meterId: ctx.state.currentMeterId,
        ...data
      })
      if (code === 200) {
        ctx.commit('SET_XXLH_LIST', [...ctx.state.xxlhList, date.list])
        ctx.commit('SET_XXLH_PARAMS', [...ctx.state.xxlhParams, date.mapArrayList[0]])
      }
    },
    changeLxlyqlState(ctx, flag) {
      ctx.commit('SET_LXLYQL_STATE', flag)
    },
    changeLxlyqlDay(ctx, day) {
      ctx.commit('SET_LXLYQL_STATE', true)
      ctx.commit('SET_LXLYQL_DAY', day)
    },
    changeXxlhList(ctx, list) {
      ctx.commit('SET_XXLH_LIST', list)
    },
    changeXxlhParams(ctx, list) {
      ctx.commit('SET_XXLH_PARAMS', list)
    },
    changeXxlhIndexs(ctx, list) {
      ctx.commit('SET_XXLH_LIST_INDEXS', list)
    },
    changeSlgjState(ctx, flag) {
      ctx.commit('SET_SLGJ_STATE', flag)
    },
    changeSlgjList(ctx, list) {
      ctx.commit('SET_SLGJ_LIST', list)
    },
    changeXxlhState(ctx, flag) {
      ctx.commit('SET_XXLH_STATE', flag)
    },
    async loadYqltjData(ctx, params) {
      const { code, date } = await fetchYqltjData({
        meterId: ctx.state.currentMeterId,
        endHours: 24,
        startHours: 1,
        ...params
      })
      if (code === 200) {
        ctx.commit('SET_YQTJ_LIST', date)
      }
    },
    async loadXsyqmsData(ctx, data) {
      const _ret = await fetchXsyqmsData({
        meterId: ctx.state.currentMeterId,
        ...data
      })
      return _ret
    },
    changeUserQueryParams(ctx, data) {
      ctx.commit('SET_USER_QUERY_PARAMS', data)
    },
    changeWbsjSelectState(ctx, flag) {
      ctx.commit('SET_WBSJ_SELECT_STATE', flag)
    },
    async loadCdrkData(ctx, params) {
      const { code, date } = await fetchCdrkData({
        meterId: ctx.state.currentMeterId,
        ...params
      })
      if (code === 200) {
        ctx.commit('SET_CDRK_LIST', date.list)
      }
    },
    changeCdrkModal(ctx, flag) {
      ctx.commit('SET_CDRK_MODAL', flag)
    },
    async loadLyqltjData(ctx, params) {
      const _ret = await fetchLyqltjData({
        meterId: ctx.state.currentMeterId,
        ...params
      })
      return _ret
    },
    async loadDsycData(ctx, params) {
      const _ret = await fetchDsycData({
        meterId: ctx.state.currentMeterId,
        ...params
      })
      return _ret
    },
    async loadSjqsData(ctx, params) {
      const _ret = await fetchSjqsData({
        meterId: ctx.state.currentMeterId,
        ...params
      })
      return _ret
    },
    async loadCcfxData(ctx, params) {
      const _ret = await fetchCcfxData({
        meterId: ctx.state.currentMeterId,
        ...params
      })
      return _ret
    },
    changeCurrentType(ctx, type) {
      ctx.commit('SET_CURRENT_TYPE', type)
    },
    changeByxs(ctx, num) {
      ctx.commit('SET_BYXS', num)
    },
    async saveParams(ctx, data) {
      const _ret = await fetchSaveParams({
        meterId: ctx.state.currentMeterId,
        ...data
      })
      return _ret
    },
    async loadLoadData(ctx, params) {
      const _ret = await fetchLoadData({
        meterId: ctx.state.currentMeterId,
        ...params
      })
      return _ret
    },
    changeSelectedTableRow(ctx, index) {
      ctx.commit('SET_SELECTED_TABLE_ROW', index)
    },
    changeSpareGas(ctx, flag) {
      ctx.commit('SET_SPARE_GAS', flag)
    },
    changeSpareGasLine(ctx, flag) {
      ctx.commit('SET_SPARE_GAS_LINE', flag)
    },
    async loadErrorTypes(ctx, params) {
      const _ret = await fetchErrorTypes()
      return _ret
    }
  }
}
